/*
 * Copyright (C) 2004, Velocityme Partnership
 * ShowNodeAction.java
 *
 * Created on 10 July 2004, 02:49
 */

package com.velocityme.www.interfaces;

//web imports
import com.velocityme.www.dto.*;
import com.velocityme.www.actionforms.*;
import com.velocityme.www.utility.NodeUtil;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;

import org.apache.commons.beanutils.*;
//ejb interfaces
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.valueobjects.NodeValue;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import org.apache.struts.actions.DispatchAction;


/**
 *
 * @author  bradloff
 */
public class ShowNodeAction extends DispatchAction  {
    
    /** Creates a new instance of ShowNodeAction */
    public ShowNodeAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        NodeIdActionForm node = (NodeIdActionForm)form;
        HttpSession session = request.getSession();
        if (node.getNodeId() == 0)
            node.setNodeId(((Integer)session.getAttribute("nodeId")).intValue());
        NodePK nodePK = new NodePK(new Integer(node.getNodeId()));
        request.setAttribute("nodePK", nodePK);
        request.setAttribute("selectedTab", "Node");
        return mapping.findForward("ACTION_NODE_SUCCESS");
    }
    
    static public void setupTreeDisplay(HttpSession session, NodeValue nodeValue, String tab) throws NamingException, CreateException, InvalidKeyException, RemoteException {
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK = nodeValue.getPrimaryKey();
        Set permissionPKs = cs.getAssignedPermissions(key, nodePK);
        session.setAttribute("nodeId", nodePK.getNodeId());
        session.setAttribute("nodeValue", nodeValue);
        if (nodeValue.getType().equals("Task") && permissionPKs.contains(new PermissionPK(new Integer(PermissionBean.TIME_CREATE))))
            session.setAttribute("quickTime", Boolean.TRUE);
        else
            session.setAttribute("quickTime", null);
        session.setAttribute("ancestorValues", cs.getVisibleAncestorValues(key, nodePK));
        setupChildrenDTOs(session, nodePK);
        session.setAttribute("tabValues", NodeUtil.getInstance().getNodeTabs(nodeValue.getFinalType()));
        boolean isValidPasteNode = false;
        boolean isValidLinkNode = false;
        boolean isValidDependencyNode = false;
        boolean isValidDependantNode = false;
        NodePK markedNodePK = (NodePK)session.getAttribute("markedNodePK");
        if (markedNodePK != null) {
            isValidPasteNode = cs.isValidPasteNode(key, nodePK, markedNodePK);
            isValidLinkNode = cs.isValidLinkNode(key, nodePK, markedNodePK);
            isValidDependencyNode = cs.isValidDependencyPair(key, markedNodePK, nodePK);
            isValidDependantNode = cs.isValidDependencyPair(key, nodePK, markedNodePK);
        }
        session.setAttribute("menuDTO", NodeUtil.getInstance().getMenu(key, nodeValue, permissionPKs, isValidPasteNode, isValidLinkNode, isValidDependencyNode, isValidDependantNode));
        session.setAttribute("buttonValues", NodeUtil.getInstance().getNodeButtons(nodeValue, tab, permissionPKs));
        session.setAttribute("selectedTabValue", tab);
    }
    
    static public void setupChildrenDTOs(HttpSession session, NodePK nodePK) throws NamingException, CreateException, InvalidKeyException, RemoteException {
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        Iterator i = cs.getVisibleChildrenValues(key, nodePK).iterator();
        session.setAttribute("childrenDTOs", getNodeDTOs(cs.getVisibleChildrenValues(key, nodePK), key, cs));
        Collection siblingDTOs = getNodeDTOs(cs.getVisibleSiblingValues(key, nodePK), key, cs);
        if (siblingDTOs != null && siblingDTOs.size() != 0)
            session.setAttribute("siblingDTOs", siblingDTOs);
        else
            session.removeAttribute("siblingDTOs");
        Collection linkedDTOs = getNodeDTOs(cs.getVisibleLinkedValues(key, nodePK), key, cs);
        if (linkedDTOs != null && linkedDTOs.size() != 0)
            session.setAttribute("linkedDTOs", linkedDTOs);
        else
            session.removeAttribute("linkedDTOs");
        Collection dependencyDTOs = getNodeDTOs(cs.getVisibleDependencyTaskValues(key, nodePK), key, cs);
        if (dependencyDTOs != null && dependencyDTOs.size() != 0)
            session.setAttribute("dependencyDTOs", dependencyDTOs);
        else
            session.removeAttribute("dependencyDTOs");
        Collection dependantDTOs = getNodeDTOs(cs.getVisibleDependantTaskValues(key, nodePK), key, cs);
        if (dependantDTOs != null && dependantDTOs.size() != 0)
            session.setAttribute("dependantDTOs", dependantDTOs);
        else
            session.removeAttribute("dependantDTOs");
        if (key.getShowBookmarks()) {
            Collection bookmarkDTOs = getNodeDTOs(cs.getBookmarkNodeValues(key), key, cs);
            session.setAttribute("bookmarkDTOs", bookmarkDTOs);
        }
        else
            session.removeAttribute("bookmarkDTOs");
    }
    
    static private Collection getNodeDTOs(Collection nodeValues, KeySession key, RemoteClientSession cs) throws InvalidKeyException, RemoteException {
        if (nodeValues == null)
            return null;
        Iterator i = nodeValues.iterator();
        Collection nodeDTOs = new ArrayList();
        while(i.hasNext()) {
            NodeValue nodeValue = (NodeValue)i.next();
            boolean isActive = false;
            if (nodeValue.getType().equals("Task"))
                isActive = cs.isTaskActive(key, new TaskPK(nodeValue.getNodeId()));
            if (nodeValue.getType().equals("User"))
                isActive = cs.isUserEnabled(key, new UserPK(nodeValue.getNodeId()));
            nodeDTOs.add(new ChildDTO(nodeValue.getFinalType(), nodeValue.getName(), nodeValue.getNodeId(), isActive));
        }
        return nodeDTOs;
    }
    
}
